<?php
//https://author.baidu.com/home/1605662285475872
//列表 //yzs ★★★抓取复杂流程的微信公众号流程
if (!function_exists('todoyzmform')) {
    function todoyzmform($result)
    {
        if (preg_match_all("/(<form[^<>]+seccodeForm[^<>]+>.*<\/form>)/Uisx", $result, $nn)) {
            //更改验证码标签
            $nn[0][0] = preg_replace_callback("/src=\"([^<>]*)\"/Uisx", function ($em) {
                $em[1] = "https://weixin.sogou.com/antispider/" . $em[1];
                preg_match("/tc=([0-9]+)/", $em[0], $nn2);
                $tc = $nn2[1];
                return "src=\"/yzm.jpg?tc={$tc}\" src_=\"{$em[1]}\" ";
            }, $nn[0][0]);

            //更改form里的样式
            $nn[0][0] = str_replace("padding-left:50px;", "padding-left:-5px;", $nn[0][0]);

            //更改action
            $nn[0][0] = str_replace("action=\"/\"", "action=\"https://weixin.sogou.com/antispider/thank.php\"", $nn[0][0]);
            $nn[0][0] = preg_replace("/(<\/form>)/Uisx", "<input type=\"hidden\" name=\"action\" value=\"https://weixin.sogou.com/antispider/thank.php\">\r\n\\1", $nn[0][0]);

            //添加referer
            preg_match("/<input\s+type=\"hidden\"\s+name=\"r\"\s+id=\"from\"\s+value=\"([^<>]*)\"\s+>/Uisx", $nn[0][0], $nn2);
            $nn[0][0] = preg_replace("/(<\/form>)/Uisx", "<input type=\"hidden\" name=\"referer\" value=\"https://weixin.sogou.com/antispider/?from=" . strtolower($nn2[1]) . "\">\r\n\\1", $nn[0][0]);

            //添加更多参数
            $nn[0][0] = preg_replace("/(<\/form>)/Uisx", "<input type=\"hidden\" name=\"v\" value=\"5\">\r\n\\1", $nn[0][0]);

            //添加仅需要的参数
            $nn[0][0] = preg_replace("/(<\/form>)/Uisx", "<input type=\"hidden\" name=\"only\" value=\"r,v,c\">\r\n\\1", $nn[0][0]);

            //添加发送验证码按键
            $nn[0][0] = preg_replace("/(<\/form>)/Uisx", "<input type=\"button\" name=\"fsyzm\" id=\"fsyzm\" value=\"发送\">\r\n\\1", $nn[0][0]);
            echo json_encode(array("retCode" => 9, "retMsg" => "请输入验证码","lists"=>array(), "template" => $nn[0][0]));
            exit(0);
        }
    }
}

if ($gather_type == 6 && preg_match("/weixin\.sogou\.com/Uisx", $this->url, $nn)) {
    $url = $this->url;
    $c = $GLOBALS['mycurl']->result("", $this->url);
    parse_str(parse_url($url)['query'], $params);
    $gzh = $params['query'];
    $cs = 0;
    do {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36");
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language:  zh-CN,zh;q=0.9"));
        if ($c->cookie != "") {
            curl_setopt($ch, CURLOPT_COOKIE, $c->cookie);
        }
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, "false");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, "2");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host:{$c->qhost}", "Connection:  keep-alive", "Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,* / *;q=0.8,application/signed-exchange;v=b3", "Accept-Language:  zh-CN,zh;q=0.9"));
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $result = curl_exec($ch);
        $c->result($result, $url);
        curl_close($ch);
        $arr = array();


        preg_match("/302\sFound|<title>404 Not Found<\/title/Uisx", $result, $nnn);
        if (($nnn[0] != "" || $c->location != "" || $reurl != "") && $cs++ < 2 && !$c->haveyzm) {
            if ($reurl != "") $url = $reurl;
            if ($c->location != "") $url = $c->location;
            $c->result("", $url);
            continue;
        } else {
            break;
        }
    } while (1);

    preg_match_all("/请输入验证码/Uisx", $result, $nnn);
    if (count($nnn[0]) > 0) {
        //列表 //内容 //yzs ★★★如果列表中出现需要验证码
        todoyzmform($result);
    }

    $pagehtml = $c->body;

    if ($c->host == "sogou.com") {
        preg_match_all("/<\!\-\-\s*a\s*\-\->(.*)<\!\-\-\s*z\s*\-\->/Uisx", $result, $nnn);
        if (count($nnn[1]) < 1) {
            echo json_encode(array("retCode" => 0, "retMsg" => "该微信号暂时没有信息！"));
            exit(0);
        }
        $result = array();
        $result['counts'] = 0;
        for ($i = 0; $i < count($nnn[1]); $i++) {
            $atitlenr = $nnn[1][$i];
            $data = array();
            $result['counts'] = $result['counts'] + 1;
            //计算链接
            preg_match("/<h3>\s+<a\s+target=\"_blank\"\s+href=\"([^<>]*)\"/Uisx", $atitlenr, $nn);
            $nn[1] = "https://weixin.sogou.com" . $nn[1] . "&query={$gzh}&k=24&h=X";
            $data['sourceurl'] = $nn[1];

            //计算来源
            preg_match("/\(来源\:(.*)\)/Uisx", $atitlenr, $nn);
            if (!empty($nn)) {
                $nn[1] = strip_tags($nn[1]);
                $data['source'] = $nn[1];
            }


            //计算标题
            preg_match("/<h3>\s+<a[^<>]*>(.*)<\/a>\s+<\/h3>/Uisx", $atitlenr, $nn);
            if (!empty($nn)) {
                $nn[1] = strip_tags($nn[1]);
                $data['title'] = $nn[1];
            }

            //计算简介
            preg_match("/<p\s+class=\"txt\-info\"[^<>]*>(.*)<\/p>/Uisx", $atitlenr, $nn);
            if (!empty($nn)) {
                $nn[1] = strip_tags($nn[1]);
                $data['info'] = $nn[1];
            }

            //计算时间
            preg_match("/document\.write\(timeConvert\('(.*)'\)\)/Uisx", $atitlenr, $nn);
            $data['dateline'] = $nn[1];

            //公众号
            $data['gzh'] = $gzh;
            //$data['content'] = $this->zqGzNr($data['sourceurl'], $data['dateline']);

            $data['url'] = $data['sourceurl'];
            $result[] = $data;
        }
        $this->result=$result;
        $GLOBALS['listdata'] = $result;
    }
}


if ($gather_type == 1 && preg_match("/weixin\.sogou\.com/Uisx", $this->url, $nn)) {
    //列表 //内容 ★★★如果抓取内容过程中出现需要验证码
    $url = $this->url;
    $c = $GLOBALS['mycurl']->result("", $this->url);
    parse_str(parse_url($url)['query'], $params);
    $gzh = $params['query'];
    $cs = 0;
    do {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
        curl_setopt($ch, CURLOPT_REFERER, "https://weixin.sogou.com/weixin?type=2&query=gxfinance");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language:  en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6,bg;q=0.5"));
        if ($c->cookie != "") {
            curl_setopt($ch, CURLOPT_COOKIE, $c->cookie);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, "false");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, "2");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host:{$c->qhost}", "Connection:  keep-alive", "Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Language:  en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6,bg;q=0.5"));
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $result = curl_exec($ch);
        curl_close($ch);
        $c->result($result, $this->url);

        if (preg_match("/window\.location\.replace/Uisx", $result)) {
            preg_match_all("/url\s+\+\=\s*\'(.*)\'\;/Uisx", $result, $nn);
            $url = "";
            for ($i = 0; $i < count($nn[1]); $i++) {
                $url .= $nn[1][$i];
            }
            $url = str_replace("@", "", $url);
        }

        preg_match("/302\sFound|<title>404 Not Found<\/title/Uisx", $result, $nnn);
        if (($nnn[0] != "" || $c->location != "") && $cs++ < 2 && !$c->haveyzm) {
            if ($c->location != "") $url = $c->location;
            continue;
        } else {
            break;
        }
    } while (1);


    preg_match_all("/请输入验证码/Uisx", $result, $nnn);
    if (count($nnn[0]) > 0) {
        todoyzmform($result);
    }
    do {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
        curl_setopt($ch, CURLOPT_REFERER, "https://weixin.sogou.com/link?url=dn9a_-gY295K0Rci_xozVXfdMkSQTLW6cwJThYulHEtVjXrGTiVgS-8PHWH7OaqtmK7XYVknWHFSG0ZQ1h-RtVqXa8Fplpd93fOYLP9_SsV-y6mXmhbguUM_YcdWKe-E35QuAKPER0ZL5SyzYAGMFqOBmlQH8602MjQRtk3cIKR2K88PJq2ZwSYyLdyo2quMmCStHW5QQXKL1LJTAkYEzPWfyMCXJCHNg3pt7KJNwOGPGxqK0Y4ZNhqbfAyJ0oR6Ul4meiKBG1t5Nu7CnoSqqg..&type=2&query=gxfinance&k=26&h=G");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language:  en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6,bg;q=0.5"));
        if ($c->cookie != "") {
            curl_setopt($ch, CURLOPT_COOKIE, $c->cookie);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, "false");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, "2");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host:  mp.weixin.qq.com", "Connection:  keep-alive", "Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Language:  en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6,bg;q=0.5"));
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $result = curl_exec($ch);
        curl_close($ch);
        $c->result($result,$url);
        preg_match("/302\sFound|<title>404 Not Found<\/title/Uisx", $result, $nnn);
        if (($nnn[0] != "" || $c->location != "") && $cs++ < 2 && !$c->haveyzm) {
            if ($c->location != "") $url = $c->location;
            continue;
        } else {
            break;
        }
    } while (1);
    $pagehtml = $c->body;
}

